<template>
  <div class="app-container">
    <el-form ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="会员卡类型" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入会员卡类型"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 240px"
          @change="handleQuery"
        >
          <el-option label="禁用" value="0" />
          <el-option label="启用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col>
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新建会员卡类型</el-button>
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
        >删除会员卡类型</el-button>
      </el-col>
    </el-row>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会员卡类型" prop="name" min-width="100" />
      <el-table-column label="充值（余额）金额" prop="chargeRange" min-width="150">
        <template #default="scope">
          <span>{{ scope.row.chargeRange.replace(/,/,'~') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="折扣">
        <template #default="scope">
          <span>{{ scope.row.discountPercent }}%</span>
        </template>
      </el-table-column>
      <el-table-column label="会员人数" prop="personCount" min-width="50" />
      <el-table-column label="使用周期" prop="usePeriodStr" min-width="250" />
      <el-table-column label="余额限制" prop="cardTypeStr">
        <template #default="scope">
          <span>{{ scope.row.balanceLimit=='11'?'购买商品与场地预定可用':(scope.row.balanceLimit=='01'?'场地预定可用':'') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期" prop="validityStr" min-width="100" />
      <el-table-column label="状态" prop="statusStr" min-width="80" />

      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding">
        <template #default="scope">
          <el-button type="text" @click="updateRow(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          <el-button type="text" @click="changeStatus(scope.row)">{{ scope.row.status==0?'启用':'禁用' }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新建会员卡/编辑会员卡 -->
    <dialogModifyCard :props="dialogProps" @submitForm="resetQuery" />
  </div>
</template>

<script setup name="Card">
import { getCurrentInstance } from 'vue'
import { onMounted, reactive } from '@vue/runtime-core'
import { toRefs } from '@vueuse/shared'
import { changeCardStatus, delCard, getCardList } from '../../../../api/ven/member'
import dialogModifyCard from '../components/dialog/modifyCard'


const { proxy } = getCurrentInstance()

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    status: undefined
  },
  loading: false,
  tableData: [],
  total: 0,
  multiple: true
})
const { queryParams, loading, tableData, total, multiple } = toRefs(data)

let selectedIds = []
const dialogPropsBase = {
  open: false,
  title: '',
  data: null
}
const dialogProps = reactive({
  ...dialogPropsBase
})


// 获取列表
function getList() {
  loading.value = true
  getCardList(queryParams.value).then(res => {
    loading.value = false
    tableData.value = res.rows
    total.value = res.total
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleSelectionChange(selection) {
  selectedIds = selection.map(item => item.id)
  multiple.value = !selection.length
}

function handleAdd() {
  _.assign(dialogProps, dialogPropsBase, {
    open: true,
    title: '新建会员卡'
  })
}

function handleDelete(row) {
  let ids, title
  if (row.id) {
    ids = row.id
    title = `是否确认删除会员卡 ${row.name}`
  } else {
    ids = selectedIds
    title = `是否确认删除 ${selectedIds.length} 个会员卡？`
  }

  proxy.$modal.confirm(title).then(function() {
    return delCard(ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  })
}

function updateRow(row) {
  _.assign(dialogProps, dialogPropsBase, {
    open: true,
    title: '编辑会员卡',
    data: { ...row }
  })
}

function changeStatus(row) {
  proxy.$modal
    .confirm(`确定操作？`)
    .then(() => {
      return changeCardStatus(row)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('操作成功')
    })
}

onMounted(() => {
  getList()
})

</script>

<style scoped lang='scss'>

</style>
